﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using Service;
using MODEL;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using System.Windows.Input;
using Microsoft.Practices.ServiceLocation;
using GalaSoft.MvvmLight.Views;
using GalaSoft.MvvmLight.Ioc;
using Common;


namespace ViewModel.SystemManagement
{
    public class UserInsertViewModel:ViewModelBase
    {
        public ViewModelBase ParentViewModel { get; set; }

        public ICommand InsertNewUserCommand { get; set; }

        private IUserService m_UserService { get; set; }

        public UserInsertViewModel(IUserService userService)
        {
            this.m_UserService = userService;

            this.InsertNewUserCommand = new RelayCommand(InsertNewUser);
        }

        private string m_Account;
        public string Account
        {
            get { return this.m_Account; }
            set { this.m_Account = value; base.RaisePropertyChanged(() => this.Account); }
        }

        private string m_Password;
        public string Password
        {
            get { return this.m_Password; }
            set { this.m_Password = value; base.RaisePropertyChanged(() => Password); }
        }

        private string m_UserName;
        public string UserName
        {
            get { return this.m_UserName; }
            set { this.m_UserName = value; base.RaisePropertyChanged(() => this.UserName); }
        }

        private async void InsertNewUser()
        {
            if (string.IsNullOrEmpty(this.Account))
            {
                await ServiceLocator.Current.GetInstance<IDialogService>().ShowMessage("用户名不能为空", "校验失败");
                return;
            }

            if (string.IsNullOrEmpty(this.Password))
            {
                await ServiceLocator.Current.GetInstance<IDialogService>().ShowMessage("密码不能为空", "校验失败");
                return;
            }

            if (string.IsNullOrEmpty(this.UserName))
            {
                await ServiceLocator.Current.GetInstance<IDialogService>().ShowMessage("姓名不能为空", "校验失败");
                return;
            }

            if (m_UserService.ExistUser(this.Account))
            {
                await ServiceLocator.Current.GetInstance<IDialogService>().ShowMessage("当前账户已存在", "校验失败");
                return;
            }

            try
            {
                Task task = this.m_UserService.AddUser(this.Account, this.Password, this.UserName);
                task.Wait();

                await ServiceLocator.Current.GetInstance<IDialogService>().ShowMessage("保存成功", "提示");
                this.MessengerInstance.Send<object>(null, "UserInsert_CloseForm");
                (this.ParentViewModel as UserManagerViewModel)?.RefreshData();

            }
            catch (Exception ex)
            {
                await ServiceLocator.Current.GetInstance<IDialogService>().ShowError("删除失败", "错误", "确定", null);
                Log.WriteException(ex);
            }
        }
    }
}
